package com.icehand.echarts.web.util;


import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;

import java.io.IOException;
import java.util.Map;

/**
 * @author zhoujunmei
 * @date 2019-08-05
 */
public class CommandExecUtil {
    public static int phantomjsExec(String pahantomjsPath, String[] args) {
        try {
            return CommandExecUtil.exec(pahantomjsPath,args,null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static int exec(String command, String[] args, Map<String, ?> substitutionMap) throws IOException {
        CommandLine commandLine = new CommandLine(command);
        if(args != null  && args.length >0){
            commandLine.addArguments(args);
        }
        if(null != substitutionMap) {
            commandLine.setSubstitutionMap(substitutionMap);
        }

        DefaultExecutor executor = new DefaultExecutor();
        return executor.execute(commandLine);
    }
}
